/**
 *  *2010-5-4 下午01:09:53  add 
 */
package com.base.myproject.client.busi.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.myproject.client.Excel.DownExcelDialog;  
import com.base.myproject.client.Excel.ExcelObject;
import com.base.myproject.client.Excel.Grid2ExcelBO;
import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncStartFlow;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * 审批列表，显示用户已经审批过，审批任务
 * 
 * @author Administrator 2010-5-4 javazgw@gmail.com
 */
public class RatifyList extends BaseJDBCPanel {

	String bcode;

	Button ratify = new Button("审批");
	TextField<String> busi = new TextField<String>();
	TextField<String> icode = new TextField<String>();
	Button make = new Button("产生流程");
	Button refresh = new Button("刷新");
	Button test	 = new Button("导出获取数据");
	ToolBar tb = new ToolBar();
	public RatifyList() {
		// 登录用户名
		this.bcode = Env.getEnvbcode();

		this.bcode="zgw";
		
		init();
	}

	public void init() {
		initWidget();
		initbutton();
		
	}
	public void complete()
	{
		//工具栏菜单
		getToolbarll().clear();
//		tb.add(busi);
//		tb.add(icode);
//		tb.add(make);
		tb.add(ratify);
		tb.add(refresh);
		//tb.add(test);
		getToolbarll().add(tb);
		//super.complete();
	}
	private void initWidget() {
		//this.setHeaderVisible(false);
		this.add(createGrid());
//	add(test);
		
		//this.setBottomComponent(tb);
	}

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("dbid_");
		type.getField("dbid_").setType(Integer.class);
		type.addField("name_");
		type.getField("name_").setType(String.class);
		type.addField("create_");
		type.getField("create_").setType(Date.class);
		type.addField("execution_id_");
		type.getField("execution_id_").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("null");
		info.setKeyname(new String[] { "icode" });
		info
				.setSql("select task.dbid_,task.name_ ,task.create_,task.execution_id_ from jbpm4_task task   left join jbpm4_participation pt on task.DBID_=pt.TASK_    where task.ASSIGNEE_='"+bcode+"' or ( pt.TYPE_ = 'candidate' and (pt.USERID_='"+bcode+"')   or pt.GROUPID_ in ('"+bcode+"'))");
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseGridPanel gridpanel = null;

	BaseGridPanel createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

//		ColumnConfig C1 = new ColumnConfig("dbid_", "dbid_", 180);
//		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("name_", "任务名称", 180);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("create_", "任务提交时间", 180);
		C3.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss"));
		columns.add(C3);
//		ColumnConfig C4 = new ColumnConfig("execution_id_", "execution_id_", 180);
//		columns.add(C4);

		ColumnModel cm = new ColumnModel(columns);
		gridpanel = new BaseGridPanel(getStore(), cm,false,false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		gridpanel.getBg().setSelectionModel(cbs);
		gridpanel.getBg().setHeight(400);
		return gridpanel;
		
		
	
	}
	void initbutton()
	{
		ratify.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
			
				String busi = null;
				String innercode=null;
				String flowname=null;
				
				String str = store.getValue("execution_id_").toString();
				GWT.log(str, null);
				String[] strarray=null;
				if(str!=null)
					strarray = str.split("\\.");
				GWT.log("length="+strarray.length, null);
				if(strarray!=null && strarray.length>=3)
				{
					flowname= strarray[0];
					busi = strarray[1];
					innercode = strarray[2];
				}
		
				if(busi==null || innercode==null || flowname==null)
				{
					MessageBox.alert("错误", "为能获取系统的代码，请联系管理员。", null);
				}
				else
				{
				
				RatifyInfo ri = new RatifyInfo();
				ri.setBusi(busi);
				ri.setFlowname(flowname);
				ri.setIcode(innercode);
				ri.setId(store.getValue("dbid_")+"");
				ri.setUserid(bcode);
				Map<String,Object> variables =new HashMap<String,Object>();
				ri.setVariables(variables);
				new BusinessRatifyDiaolog(ri);
				}
			}
			
		});
		make.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				CommandSyncContainer list = new CommandSyncContainer();
				
				CommandSyncStartFlow ratifystart = new CommandSyncStartFlow();
				
				list.add(ratifystart);
				RatifyInfo ratifyinfo = new RatifyInfo();
				ratifyinfo.setBusi(busi.getValue());
				ratifyinfo.setIcode(icode.getValue());
				ratifyinfo.setFlowname("simple1");
				//ratifyinfo.getVariables().put("content", "to task2");
				ratifystart.setRatifyinfo(ratifyinfo);
				com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.SendCommandSync(list, new AsyncCallback<CommandSyncReturnObject> ()
				{

					@Override
					public void onFailure(Throwable caught) {
						
						MessageBox.alert("错误","网络连接错误，请重新尝试", null);
					}

					@Override
					public void onSuccess(
							CommandSyncReturnObject result) {
						if(result.isB())
						{
							Info.display("", "提交成功");
						}
						else
						{
							MessageBox.alert("错误", result.getMessage(), null);
						}
					}
			
				});
				
			}
			
		});
		refresh.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				store.reload();
			}
			
		});
		
		test.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
			
//				
//				ExcelObject eo  = Grid2ExcelBO.getInstance().getEO(gridpanel.getBg());
//			DownExcelDialog.getInstance().javascript(eo);
				GWT.log(""+gridpanel.getBg().getColumnModel().getColumn(2).isHidden());
				gridpanel.getBg().getColumnModel().getColumn(2).setHidden(true);
				GWT.log(""+gridpanel.getBg().getColumnModel().getColumn(2).isHidden());
				gridpanel.getBg().recalculate();
				gridpanel.getBg().repaint();
			//	gridpanel.getBg().getView().onHiddenChange(gridpanel.getBg().getColumnModel(), 2, true);
				
				
			}});
		
	
	}
}
